﻿using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace bonus.Controllers
{
	public class ProductController : Controller
	{
		/// <summary>
		/// 产品页面
		/// </summary>
		/// <returns></returns>
		// GET: Product
		public ActionResult Index()
		{
			return View();
		}

		/// <summary>
		/// 参加产品页面
		/// </summary>
		/// <returns></returns>
		public ActionResult AddProduct()
		{
			return View();
		}
		/// <summary>
		/// 产品月数据
		/// </summary>
		/// <returns></returns>
		public ActionResult ProductMonthData()
		{
			return View();
		}

		/// <summary>
		/// 添加产品
		/// </summary>
		/// <param name="no">产品编号</param>
		/// <param name="name">产品名称</param>
		/// <param name="money">产品价格</param>
		/// <returns></returns>
		[HttpPost]
		public JsonResult AddProduct(string no, string name, string money, int pv)
		{
			Repository.Repository tool = new Repository.Repository();

			var user = tool.GetOne<Models.User>(Services.Cookie.GetId());

			if (user == null || user.UserType != Models.User.TypeClass.ADMIN)
			{
				return Json(new { Code = "FAIL", Msg = "用户权限不够或未登陆" });
			}

			var filter = Builders<Models.Products>.Filter.Where(p => p.ProNo == no);

			var count = tool.Count(filter);

			if (count > 0)
			{
				return Json(new { Code = "FAIL", Msg = "编号存在" });
			}
			Models.Products pro = new Models.Products();
			pro._id = ObjectId.GenerateNewId();
			pro.AddDT = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
			pro.Money = Convert.ToDecimal(money);
			pro.ProNo = no;
			pro.Name = name;
			pro.PV = pv;

			var result = tool.AddOneAsync(pro);
			return Json(new { Code = "OK", Msg = "添加成功" });
		}


		/// <summary>
		/// 产品分页
		/// </summary>
		/// <param name="index">当前页数</param>
		/// <param name="size">每页数量</param>
		/// <returns></returns>
		[HttpPost]
		public JsonResult ProcudtPage(int index = 1, int size = 10)
		{
			Repository.Repository tool = new Repository.Repository();

			var user = tool.GetOne<Models.User>(Services.Cookie.GetId());

			if (user == null || user.UserType != Models.User.TypeClass.ADMIN)
			{
				return Json(new { Code = "FAIL", Msg = "用户权限不够或未登陆" });
			}

			var count = tool.Count<Models.Products>(new BsonDocument());

			var page = Math.Ceiling(Convert.ToDouble(count) / size);//总页数

			var list = tool.GetMany<Models.Products>(new BsonDocument(), "AddDT:-1", size, index);

			if (list == null)
			{
				list = new List<Models.Products>();
			}

			return Json(new { Code = "OK", Data = list, total = count });//Totle:总条数  Count:总页数   Index 当前页

		}


		/// <summary>
		/// 删除产品
		/// </summary>
		/// <param name="proNo">产品编号</param>
		/// <returns></returns>
		[HttpPost]
		public JsonResult DelPro(string proNo)
		{
			Repository.Repository tool = new Repository.Repository();

			var user = tool.GetOne<Models.User>(Services.Cookie.GetId());

			if (user == null || user.UserType != Models.User.TypeClass.ADMIN)
			{
				return Json(new { Code = "FAIL", Msg = "用户权限不够或未登陆" });
			}

			var filter = Builders<Models.Products>.Filter.Where(p => p.ProNo == proNo);

			var result = tool.DeleteOneAsync(filter);

			return Json(new { Code = "OK", Msg = "删除成功" });

		}


		/// <summary>
		/// 订单分页数据 
		/// </summary>
		/// <param name="key">关键字</param>
		/// <param name="index">当前页数</param>
		/// <param name="size">每页数量</param>
		/// <returns></returns>
		[HttpPost]
		public JsonResult MonthPro(string key, int index = 1, int size = 10)
		{
			Repository.Repository tool = new Repository.Repository();

			var user = tool.GetOne<Models.User>(Services.Cookie.GetId());

			if (user == null || user.UserType != Models.User.TypeClass.ADMIN)
			{
				return Json(new { Code = "FAIL", Msg = "用户权限不够或未登陆" });
			}


			if (string.IsNullOrEmpty(key))
			{
				var count = tool.Count<Models.MonthPro>(new BsonDocument());

				var page = Math.Ceiling(Convert.ToDouble(count) / size);//总页数

				var list = tool.GetMany<Models.MonthPro>(new BsonDocument(), "AddDT:-1", size, index);

				ICollection<object> Entities = new List<object>() { };
				foreach (var item in list)
				{

					object i = new
					{
						ProNo = item.ProNo,
						DataStr = item.DataStr,
						ProName = item.ProName,
						Money = item.Money.ToString("0.##"),
						TotlMoney = item.TotlMoney.ToString("0.##"),
						Num = item.Num,
					};
					Entities.Add(i);

				}
				return Json(new { Code = "OK", Data = Entities.Reverse(), total = count });//Totle:总条数  Count:总页数   Index 当前页
			}
			else
			{
				var filter = Builders<Models.MonthPro>.Filter.Where(p => p.ProNo == key);

				var count = tool.Count<Models.MonthPro>(filter);

				var page = Math.Ceiling(Convert.ToDouble(count) / size);//总页数

				var list = tool.GetMany<Models.MonthPro>(filter, "AddDT:-1", size, index);

				ICollection<object> Entities = new List<object>() { };
				foreach (var item in list)
				{

					object i = new
					{
						ProNo = item.ProNo,
						DataStr = item.DataStr,
						ProName = item.ProName,
						Money = item.Money.ToString("0.##"),
						TotlMoney = item.TotlMoney.ToString("0.##"),
						Num = item.Num,
					};
					Entities.Add(i);

				}
				return Json(new { Code = "OK", Data = Entities.Reverse(), total = count });//Totle:总条数  Count:总页数   Index 当前页
			}
		}


		/// <summary>
		/// 产品下架/上架
		/// </summary>
		/// <param name="proId">产品标识</param>
		/// <returns></returns>
		public JsonResult UnSale(string proNo)
		{
			Repository.Repository tool = new Repository.Repository();
			var filter = Builders<Models.Products>.Filter.Where(p => p.ProNo == proNo);
			var entity = tool.GetOne(filter);
			if (entity == null)
			{
				return Json(new { Code = "FAIL", Msg = "产品不存在" });
			}

			var updatau = Builders<Models.Products>.Update.Set("UnSale", !entity.UnSale);

			var resultu = tool.UpdateOne(entity._id.ToString(), updatau);

			return Json(new { Code = "OK", Msg = "已" + (entity.UnSale ? "上架" : "下架")/*, Title = !entity.UnSale ? "下架" : "上架" */});
		}

	}
}